package com.hjn.fastdfs.controller;

import com.hjn.fastdfs.pojo.FastDFSFile;
import com.hjn.fastdfs.util.FastDFSClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.InputStream;

@Controller
@Slf4j
public class UploadController {


    @Value("${fastdfs.ip}")
    private String fastdfsUrl;

    /**
     * 页面跳转
     *
     * @return
     */
    @GetMapping("/")
    public String index() {
        return "upload";
    }

    /**
     * 上传文件
     *
     * @param file
     * @param redirectAttributes
     * @return
     */
    @PostMapping("/upload")
    public String singleFileUpload(@RequestParam("file") MultipartFile file,
                                   RedirectAttributes redirectAttributes) {
        if (file.isEmpty()) {
            redirectAttributes.addFlashAttribute("message", "Please select a file to upload");
            return "redirect:uploadStatus";
        }
        try {
            // 上传文件拿到返回的文件路径
            String path = saveFile(file);
            redirectAttributes.addFlashAttribute("message", "You successfully uploaded '" + file.getOriginalFilename() + "'");
            redirectAttributes.addFlashAttribute("path", "file path url '" + path + "'");
        } catch (Exception e) {
            log.error("upload file failed", e);
        }
        return "redirect:/uploadStatus";
    }

    /**
     * 上传文件
     *
     * @param multipartFile
     * @return
     * @throws Exception
     */
    public String saveFile(MultipartFile multipartFile) throws Exception {
        String[] fileAbsolutePath = {};
        String fileName = multipartFile.getOriginalFilename();
        log.info("fileName:" + fileName);
        //文件类型
        String ext = fileName.substring(fileName.lastIndexOf(".") + 1);
        log.info("ext:" + ext);
        byte[] file_buff = null;
//        获取文件流
        InputStream inputStream = multipartFile.getInputStream();

        if (inputStream != null) {
            int len1 = inputStream.available();
            file_buff = new byte[len1];
            inputStream.read(file_buff);
        }
        log.info("file_buff:" + file_buff.toString());
        inputStream.close();
        FastDFSFile file = new FastDFSFile(fileName, file_buff, ext);
        //上传文件
        try {
            fileAbsolutePath = FastDFSClient.upload(file);
        } catch (Exception e) {
            log.error("upload file Exception!", e);
        }

        if (fileAbsolutePath == null) {
            log.error("upload file failed,please upload again!");
        }

        log.info(fastdfsUrl);
        String path = FastDFSClient.getTrackerUrl() + fileAbsolutePath[0] + "/" + fileAbsolutePath[1];
        log.info("path:" + path);
//        String path=fastdfsUrl + "/" +fileAbsolutePath[0]+ "/"+fileAbsolutePath[1];
        return path;
    }


    /**
     * 页面跳转
     *
     * @return
     */
    @GetMapping("/uploadStatus")
    public String uploadStatus() {
        return "uploadStatus";
    }

}